<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理后台登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background-color: #d7ebf5;
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-container {
            background: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 400px;
        }

        h2 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #34495e;
            font-weight: 500;
        }

        input {
            width: 100%;
            padding: 10px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }

        input:focus {
            outline: none;
            border-color: #3498db;
        }

        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        button {
            margin-top: 10px;
            width: 100%;
            padding: 10px;
            background-color: #2980b9;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #3498db;
        }

        button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
        }

        .loading {
            position: relative;
            pointer-events: none;
        }

        .loading::after {
            content: "";
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            width: 20px;
            height: 20px;
            border: 2px solid rgba(255, 255, 255, 0.3);
            border-top-color: white;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            from {
                transform: translateY(-50%) rotate(0deg);
            }
            to {
                transform: translateY(-50%) rotate(360deg);
            }
        }

        .remember-forgot {
            float: right;
            margin-bottom: 15px;
            font-size: 14px;
        }

        a {
            color: #2980b9;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>管理后台登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">账号</label>
            <input type="text" id="username" name="username" placeholder="请输入账号">
            <div class="error-message" id="usernameError">请输入有效的账号</div>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码">
            <div class="error-message" id="passwordError">请输入密码</div>
        </div>
        <button type="submit" id="submitBtn">登录</button>
    </form>
</div>

<script>
    const form = document.getElementById('loginForm');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    const submitBtn = document.getElementById('submitBtn');

    // 表单验证
    function validateForm1() {
        let isValid = true;

        // 验证用户名
        if (usernameInput.value.trim() === '') {
            showError(usernameInput, 'usernameError', '请输入账号');
            isValid = false;
        } else {
            hideError(usernameInput, 'usernameError');
        }
        return isValid;
    }

    // 表单验证
    function validateForm2() {
        let isValid = true;
        // 验证密码
        if (passwordInput.value.trim() === '') {
            showError(passwordInput, 'passwordError', '请输入密码');
            isValid = false;
        } else {
            hideError(passwordInput, 'passwordError');
        }

        return isValid;
    }

    function showError(input, errorId, message) {
        const errorElement = document.getElementById(errorId);
        input.style.borderColor = '#e74c3c';
        errorElement.textContent = message;
        errorElement.style.display = 'block';
    }

    function hideError(input, errorId) {
        const errorElement = document.getElementById(errorId);
        input.style.borderColor = '#e0e0e0';
        errorElement.style.display = 'none';
    }


    /**
     * 登录
     */
    function login() {
        let url = `${SYS_API_PREFIX}/auth/admin/login`;
        let data = JSON.stringify({
            account: usernameInput.value,
            password: passwordInput.value
        });
        ajaxRequestNotAuth(url, METHOD.POST, data, CONTENT_TYPE_JSON, function (res) {
            if (res.code !== 200) {
                submitBtn.disabled = false;
                submitBtn.classList.remove('loading');
                showError(passwordInput, 'passwordError', res.message);
                console.error("获取数据失败,res:", res);
                return;
            }
            // 保存token
            saveTokenToCookie(res.data);
            localStorage.setItem(USER_KEY, JSON.stringify(res.data.user));
            location.href = '/index.html';
        });
    }

    // 表单提交
    form.addEventListener('submit', async (e) => {
        e.preventDefault();

        if (!validateForm1()) return;
        if (!validateForm2()) return;

        submitBtn.disabled = true;
        submitBtn.classList.add('loading');
        login();
    });

    // 实时验证
    usernameInput.addEventListener('blur', validateForm1);
    passwordInput.addEventListener('blur', validateForm2);
</script>

<script src="scripts/jquery-3.7.1.min.js" type="text/javascript"></script>
<script src="scripts/jquery.cookie-1.4.1.min.js" type="text/javascript"></script>
<script src="res/js/common.js" type="text/javascript"></script>
</body>
</html>